from django.db import models
from sku.models import Good
from user.models import User

# 优惠类型
# type_name  varchar(30)  '优惠类型名称'
class CouponType(models.Model):
    """
    优惠类型
    """
    type_name = models.CharField(max_length=30, verbose_name='优惠类型名称')

    def __str__(self):
        return self.type_name

    class Meta:
        db_table = 'coupon_type'
        verbose_name_plural = "优惠券类型"


class Coupon(models.Model):
    """优惠券"""
    name = models.CharField(max_length=100, verbose_name='优惠券名称')
    brief = models.TextField(verbose_name='优惠券介绍')
    coupon_type = models.ForeignKey(CouponType,on_delete=models.CASCADE,  blank=True, null=True)
    good = models.ForeignKey(Good,on_delete=models.CASCADE, verbose_name='商品ID', blank=True, null=True, help_text='某个商品id')
    coupon_num = models.IntegerField(verbose_name='张数', default=0)
    start_time = models.DateTimeField(verbose_name='优惠券开始使用时间')
    over_time = models.DateTimeField(verbose_name='优惠券结束使用时间')
    start_get_time = models.DateTimeField(verbose_name='开始领卷的时间')
    end_get_time = models.DateTimeField(verbose_name='领卷的结束时间')
    remark = models.CharField(max_length=300, verbose_name='备注', null=True, blank=True)

    class Meta:
        db_table = 'coupon'

    def __str__(self):
        return self.name

class CouponTypeValue(models.Model):
    """
    优惠券类型具体的值
    """
    #优惠券
    coupon = models.ForeignKey(Coupon, on_delete=models.CASCADE)
    #优惠券类型
    coupon_type = models.ForeignKey(CouponType, on_delete=models.CASCADE)
    # 类型的值
    value = models.IntegerField(verbose_name="具体的值", default=0)

    def __str__(self):
        return self.value

    class Meta:
        db_table = 'coupon_value'


class GetCoupon(models.Model):
    """
    用户领券表
    """
    user = models.ForeignKey(User, verbose_name='拥有者', on_delete=models.CASCADE)
    coupon = models.ForeignKey(Coupon, on_delete=models.CASCADE)

    STATUS_CHOICE = ((0, '未使用'), (1, '已使用'), (2, '已过期'))
    status = models.IntegerField(choices=STATUS_CHOICE, verbose_name='优惠券状态', default=0)
    get_time = models.DateTimeField(verbose_name='领券时间', auto_now_add=True)
    use_time = models.DateTimeField(verbose_name='使用时间', blank=True, null=True)

    class Meta:
        db_table = "user_coupon"
        verbose_name_plural = '用户持券表'

    def __str__(self):
        return '{}-{}-{}'.format(self.user.username, self.coupon.name, self.get_status_display())